
package me.aip.mov.util;

import java.security.SecureRandom;
import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.Validate;

public class RandomUtil{
	private static char[] codeSequence =
	{ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5',
			'6', '7', '8', '9' };

	private static char[] numSequence =
	{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

	private static SecureRandom random = new SecureRandom();
	/**
	 * 获取一个随机的QR码，14位，区分大小写
	 * @return
	 */
	public static String getRandomChars()
	{
		Random random = new Random();
		StringBuffer sBuffer = new StringBuffer();
		for (int i = 0; i < 14; i++)
		{
			sBuffer.append(codeSequence[random.nextInt(62)]);
		}
		return sBuffer.toString();
	}

	/**
	 * 获取指定长度的字符串
	 * @param length
	 * @return
	 */
	public static String getRandomChars(int length)
	{
		Random random = new Random();
		StringBuffer sBuffer = new StringBuffer();
		if (length < 1)
			length = 14;
		for (int i = 0; i < length; i++)
		{
			sBuffer.append(codeSequence[random.nextInt(62)]);
		}
		return sBuffer.toString();
	}

	public static String getRandomNumbers(int length)
	{
		Random random = new Random();
		StringBuffer sBuffer = new StringBuffer();
		if (length < 1)
			length = 14;
		for (int i = 0; i < length; i++)
		{
			sBuffer.append(numSequence[random.nextInt(10)]);
		}
		return sBuffer.toString();
	}
    /**
     * 生成随机字符串
     * @param numBytes
     * @return
     */
	public static String generateRandomString(int numBytes) {
		Validate.isTrue(numBytes > 0, "numBytes argument must be a positive integer (1 or larger)", numBytes);

		byte[] bytes = new byte[numBytes];
		random.nextBytes(bytes);
		return Hex.encodeHexString(bytes);
	}

	public static void main(String[] args) {
		System.out.println(getRandomChars(10));
	}
}
